package com.atguigu.gmall.product.controller;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.FaseDfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/admin/product/")
public class FileContoller {
    @Value("${image.url}")
    private String imgurl;
    /**
     * 图片上传接口
     * @param file 传递过来的图片文件
     * @return
     */
    @PostMapping("/fileUpload")
    private Result fileUpload( @RequestParam MultipartFile file){
        String FileId = null;
        try {
            // 转换为 byte 数组
            byte[] fileBytes = file.getBytes();
            String originalFilename = file.getOriginalFilename();
            //文件后缀名
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            System.out.println(extension);
            FileId = FaseDfsUtil.uplodeFile(fileBytes, extension);
            // System.out.println(imageUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok(imgurl+FileId);
    }

}
